package com.zw.yzk.kabu.view.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.TextView;

import com.zw.yzk.kabu.R;
import com.zw.yzk.kabu.base.BaseActivity;
import com.zw.yzk.kabu.data.cache.db.DataBaseManager;
import com.zw.yzk.kabu.data.cache.db.User;
import com.zw.yzk.kabu.data.utils.FileUtils;
import com.zw.yzk.kabu.internal.component.DaggerSettingsCompomnent;
import com.zw.yzk.kabu.internal.module.ActivityModule;
import com.zw.yzk.kabu.internal.module.NotifySwitchModule;
import com.zw.yzk.kabu.internal.module.SetNotifyModule;
import com.zw.yzk.kabu.management.ToastManager;
import com.zw.yzk.kabu.management.ToolbarBuilder;
import com.zw.yzk.kabu.presenter.SettingsPresenter;
import com.zw.yzk.kabu.view.view.SettingsView;
import com.zw.yzk.kabu.view.widget.CustomSwitch;
import com.zw.yzk.kabu.view.widget.LogoutDialog;

import java.io.File;

import javax.inject.Inject;

import butterknife.Bind;
import butterknife.OnClick;

/**
 * Created by zhanwei on 2017/1/10.
 */

public class SettingsActivity extends BaseActivity implements SettingsView {

    @Bind(R.id.modify_password)
    View modifyPassword;
    @Bind(R.id.message_notify)
    CustomSwitch notifySwitch;
    @Bind(R.id.comment_notify)
    CustomSwitch commentNotify;
    @Bind(R.id.concern_notify)
    CustomSwitch concernNotify;
    @Bind(R.id.phone)
    TextView phone;


    @Inject
    SettingsPresenter presenter;

    @Override
    public int getLayoutId() {
        return R.layout.activity_settings;
    }

    @Override
    public void iniToolbar(BaseActivity activity) {
        ToolbarBuilder
                .createBuilder(this)
                .setTitle(getResources().getString(R.string.settings))
                .build();
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        User user = DataBaseManager.getInstance().getLoginUser();
        if (user == null || user.isWechatLogin()) {
            modifyPassword.setVisibility(View.GONE);
        }
        if (user != null) {
            phone.setText(user.getPhone());
        }

        notifySwitch.setSwitchStatus(true);
        commentNotify.setSwitchStatus(true);
        concernNotify.setSwitchStatus(true);

        presenter.getSwitchStatus();
        notifySwitch.setOnStateChageListener(new CustomSwitch.OnStateChangeListener() {
            @Override
            public void OnStateChange(boolean status) {
                presenter.setSwitchStatus(1, status ? 1 : 0);
            }
        });
        concernNotify.setOnStateChageListener(new CustomSwitch.OnStateChangeListener() {
            @Override
            public void OnStateChange(boolean status) {
                presenter.setSwitchStatus(3, status ? 1 : 0);
            }
        });
        commentNotify.setOnStateChageListener(new CustomSwitch.OnStateChangeListener() {
            @Override
            public void OnStateChange(boolean status) {
                presenter.setSwitchStatus(2, status ? 1 : 0);
            }
        });
    }

    @Override
    public void initComponent() {
        DaggerSettingsCompomnent
                .builder()
                .applicationComponent(getApplicationComponent())
                .activityModule(new ActivityModule(this))
                .setNotifyModule(new SetNotifyModule())
                .build()
                .inject(this);
        presenter.setView(this);
    }

    @OnClick(R.id.shipping_address)
    public void shippingAddressClicked() {
        startActivity(new Intent(this, ShippingAddressActivity.class));
    }

    @OnClick(R.id.modify_password)
    public void modifyPassword() {
        startActivity(new Intent(this, ModifyPasswordActivity.class));
    }

    @OnClick(R.id.clear_cache)
    public void clearCache() {
        FileUtils.clearFileCache((new File(FileUtils.getCacheDir(this))));
        ToastManager.getInstance().showToast(SettingsActivity.this, getResources().getString(R.string.clear_success));
    }

    @OnClick(R.id.bing_phone)
    public void bindPhone() {
        if (!TextUtils.isEmpty(phone.getText().toString())) {
            return;
        }
        startActivity(new Intent(this, BindPhoneActivity.class));
    }

    @OnClick(R.id.log_out)
    public void logOut() {
        new LogoutDialog().showLogoutDialog(this);
    }

    @Override
    public void getNotifyStatus(boolean exhibitionNotify, boolean concernNotify, boolean commentNotify) {
        this.notifySwitch.setSwitchStatus(exhibitionNotify);
        this.commentNotify.setSwitchStatus(concernNotify);
        this.concernNotify.setSwitchStatus(commentNotify);
    }


}
